package com.community.contents.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.community.common.core.utils.Result;
import com.community.common.security.dto.UserLoginDto;
import com.community.contents.entity.UserEntity;
import com.community.contents.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private IUserService userService;

    @GetMapping("/getUserByMobile")
    public UserEntity getUserByMobile(@RequestParam("mobile") String mobile){
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        return userService.getOne(queryWrapper);
    }

    @PostMapping("save")
    public UserEntity save(@RequestBody UserEntity userEntity){
        boolean save = userService.save(userEntity);
        return userEntity;
    }


    /**
     * 修改用戶头像
     * @param userEntity
     * @return
     */
    @PostMapping("/currentUserInfo")
    public boolean currentUserInfo(@RequestBody UserEntity userEntity){
        boolean b = userService.updateById(userEntity);
        return b;
    }


    /**
     * 修改用户名/性别/生日/个性签名/标签
     * @param userEntity
     * @return
     */
    @PostMapping("userInfoEdit")
    public Result userInfoEdit(@RequestBody UserEntity userEntity, UserLoginDto userLoginDto){
        Integer userId = userLoginDto.getUserId();
        userEntity.setId(userId);
        boolean update = userService.updateById(userEntity);
        UserEntity userEntitybyid = userService.getById(userId);
        return new Result().ViewOk("修改成功！").ok(userEntitybyid);
    }


    @GetMapping("/userInfoById")
    public UserEntity userInfoById(@PathVariable Integer user_id){
        return userService.getById(user_id);
    }

    @GetMapping("/userInfoById2")
    public UserEntity userInfoById2(@RequestParam("user_id") Integer user_id){
        return userService.getById(user_id);
    }


}
